﻿using System;

namespace OscarClover.Packets.SignOn
{
    public class CliSignOn : Flap
    {
        public CliSignOn(byte[] autorizationCookie):base(FrameTypes.SignOn)
        {
            AddData(new byte[] { 0x00, 0x00, 0x00, 0x01 });
            AddTlv(new Tlv(0x06,autorizationCookie));
        }

        public CliSignOn(String uin, String nonRoastedPassword, String clientId, ushort majorVersion,
                         ushort minorVersion, ushort lesserVersion, ushort buildNumber, ushort clientIdNumber,
                         uint clientDistributionNumber, String clientLanguage, String clientCountry)
            : base(FrameTypes.SignOn)
        {
            AddData(new byte[] {0x00, 0x00, 0x00, 0x01});
            AddTlv(new Tlv(0x0001, uin));
            var roastedPassword = new byte[nonRoastedPassword.Length];
            var roastArray = new byte[]
                                 {
                                     0xF3, 0x26, 0x81, 0xC4, 0x39, 0x86, 0xDB, 0x92, 0x71, 0xA3, 0xB9, 0xE6, 0x53, 0x7A,
                                     0x95, 0x7C
                                 };
            for (int i = 0; i < roastedPassword.Length; i++)
            {
                roastedPassword[i] = (byte) (((byte) nonRoastedPassword[i]) ^ roastArray[i]);
            }
            AddTlv(new Tlv(0x0002, roastedPassword));
            AddTlv(new Tlv(0x0003, clientId));
            AddTlv(new Tlv(0x0017, majorVersion));
            AddTlv(new Tlv(0x0018, minorVersion));
            AddTlv(new Tlv(0x0019, lesserVersion));
            AddTlv(new Tlv(0x001a, buildNumber));
            AddTlv(new Tlv(0x0016, clientIdNumber));
            AddTlv(new Tlv(0x0014, clientDistributionNumber));
            AddTlv(new Tlv(0x000f, clientLanguage));
            AddTlv(new Tlv(0x000e, clientCountry));
        }
    }
}